/**
 * Copyright 2017 Confluent Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

package io.confluent.ksql.rest.server;

import com.github.rvesse.airline.HelpOption;
import com.github.rvesse.airline.SingleCommand;
import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option;
import com.github.rvesse.airline.annotations.restrictions.Once;
import com.github.rvesse.airline.annotations.restrictions.Required;
import com.github.rvesse.airline.help.Help;
import com.github.rvesse.airline.parser.errors.ParseException;

import javax.inject.Inject;
import java.io.IOException;

// TODO: Enable specification of properties here so that useful defaults for bootstrap server, port,
// etc. can be established in the ksql-server-start script

@Command(name = "KSQL-Server", description = "KSQL Cluster")
public class CliOptions {

  private static final String QUICKSTART_OPTION_NAME = "--quickstart";

  // Only here so that the help message generated by Help.help() is accurate
  @Inject
  public HelpOption help;

  @Once
  @Required
  @Arguments(
      title = "properties-file",
      description = "A file specifying properties for the KSQL Server, KSQL, "
          + "and its underlying Kafka Streams instance(s)"
  )
  private String propertiesFile;

  public String getPropertiesFile() {
    return propertiesFile;
  }

  @Option(
      name = QUICKSTART_OPTION_NAME,
      description = "Whether to serve a quickstart file (located in the resources directory) "
          + "at /quickstart.html"
  )
  private boolean quickstart;

  public boolean getQuickstart() {
    return quickstart;
  }

  public static CliOptions parse(String[] args) throws IOException {

    SingleCommand<CliOptions> optionsParser = SingleCommand.singleCommand(CliOptions.class);

    // If just a help flag is given, an exception will be thrown due to missing required options;
    // hence, this workaround
    for (String arg : args) {
      if ("--help".equals(arg) || "-h".equals(arg)) {
        Help.help(optionsParser.getCommandMetadata());
        return null;
      }
    }

    try {
      return optionsParser.parse(args);
    } catch (ParseException exception) {
      if (exception.getMessage() != null) {
        System.err.println(exception.getMessage());
      } else {
        System.err.println("Options parsing failed for an unknown reason");
      }
      System.err.println("See the -h or --help flags for usage information");
      return null;
    }
  }
}
